package ro.gillardon.beans;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import ro.gillardon.commons.util.DateUtils;

@ManagedBean(name = "calendarView", eager = true)
@SessionScoped
public class CalendarView {

  private Date fromDate;
  private Date toDate;

  /**
   * Instantiates a new CalendarView.
   */
  public CalendarView() {
    // constructor without parameters
  }

  public void onDateSelect(final SelectEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    facesContext.addMessage(null,
      new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected",
        format.format(event.getObject())));
  }

  public void click() {
    RequestContext requestContext = RequestContext.getCurrentInstance();

    requestContext.update("form:display");
    requestContext.execute("PF('dlg').show()");
  }

  public Date getFromDate() {
    return DateUtils.createNewDate(this.fromDate);
  }

  public void setFromDate(final Date fromDate) {
    this.fromDate = DateUtils.createNewDate(fromDate);
  }

  public Date getToDate() {
    return DateUtils.createNewDate(this.toDate);
  }

  public void setToDate(final Date toDate) {
    this.toDate = DateUtils.createNewDate(toDate);
  }

}
